<?php

namespace App\Http\Controllers\Index\Users;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Users\UserAddress;
use App\Http\Requests\Index\Users\UserAddressRequest;
use UserAddressesSave;
class UserAddressesController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    public function index(Request $request)
    {
        return view('index.user_addresses.index', [
            'addresses' => $request->user()->addresses,
        ]);
    }
    public function create()
    {
        return view('index.user_addresses.create_and_edit', ['address' => new UserAddress()]);
    }
    public function store(UserAddressRequest $request)
    {
        UserAddressesSave::store($request);

        return redirect()->route('user_addresses.index');
    }
    public function edit(UserAddress $user_address)
    {
        $this->authorize('own', $user_address);
        return view('index.user_addresses.create_and_edit', ['address' => $user_address]);
    }
    public function update(UserAddress $user_address, UserAddressRequest $request)
    {
        $this->authorize('own', $user_address);
        UserAddressesSave::update($request,$user_address);
        return redirect()->route('user_addresses.index');
    }
    public function destroy(UserAddress $user_address)
    {
        $this->authorize('own', $user_address);
        UserAddressesSave::destroy($user_address);
        // 把之前的 redirect 改成返回空数组
        return [];
    }
}
